import { Store } from 'store-model.js';
var QQMapWX = require('../../libs/qqmap-wx-jssdk.min.js');
var demo = new QQMapWX({
  key: 'JCQBZ-DZQ6P-ZJEDL-LROIS-3K6Y2-3NF6R' // 必填
});
var store=new Store();  //实例化 home 的推荐页面
Page({
  data: {
    // transClassArr:['tanslate0','tanslate1','tanslate2','tanslate3','tanslate4','tanslate5'],
    currentMenuIndex:0,
    loadingHidden:false,
  },
  onLoad: function () {
    this._loadData();
  },

  /*加载所有数据*/
  _loadData:function(callback){
    var that = this;
    store.getStoreData((storeData)=>{
      var num = new Array();
      // var number;
      for(var i in storeData){
        // console.log(categoryData[i]);
        num[i] = 'tanslate'+i;
      }
      // console.log(storeData);
      that.setData({
        storeDataAll: storeData,
        transClassArr:num,
        loadingHidden: true
      });
      that.getProductsByCategory(storeData[0].id,(data)=>{
        // console.log(data);
        var dataObj= {
          storeInfo: data,
          title: storeData[0].name
        };
        that.setData({
          loadingHidden: true,
          storeInfo:dataObj
        });
        callback&& callback();
      });
    });
  },

  /*切换分类*/
  changeStore:function(event){
    var index=store.getDataSet(event,'index'),
        id=store.getDataSet(event,'id')//获取data-set
    this.setData({
      currentMenuIndex:index
    });

    //如果数据是第一次请求
   
      var that=this;
      // console.log(id);
      this.getProductsByCategory(id, (data)=> {
        that.setData(that.getDataObjForBind(index,data));
      });
   
  },

 
  getDataObjForBind:function(index,data){
    var obj={},
        arr=[],
       
        baseData=this.data.storeDataAll[index];
        for(var i in this.data.storeDataAll){
          arr[i] = i;
        }
        // console.log(this.data.categoryTypeArr);
        // console.log(arr);
    for(var item in arr){
      if(item==arr[index]) {
        obj['storeInfo']={
          storeInfo:data,

          title:baseData.name
        };
        // console.log(obj.storeInfo);
        return obj;
      }
    }
  },

  getProductsByCategory:function(id,callback){
    store.getStoreInfos(id,(data)=> {
      callback&&callback(data);
    });
  },



  mapS:function(e){
    var that = this;
    var longitude;
    var latitude;
        demo.geocoder({
        address: e.currentTarget.dataset.name,
      success: function(ress) {
        longitude = ress.result.location.lng;
        latitude = ress.result.location.lat;
        wx.getLocation({
          // type: 'wgs84', //返回可以用于wx.openLocation的经纬度  
          success: function (res) {
            
            longitude: longitude;
            latitude: latitude;
            wx.openLocation({
              latitude: latitude,
              longitude: longitude,
              name: e.currentTarget.dataset.name,
              scale: 28
            })
          }
        })  
      //  markers:
        // console.log(res);
        // that.setData({
        //     longitude:res.result.location.lng,
        //     latitude:res.result.location.lat,
        // //     markers: [{
        // //  // iconPath: "../../imgs/icon/map.png",
        // //   id: 0,
        // //   latitude: res.result.location.lat,
        // //   longitude: res.result.location.lng,
        // //   width: 35,
        // //   height: 45
        // // }]
        //   })
        // console.log(this.data.longitude);
      },

  });

    
    // console.log(e.currentTarget.dataset.name);
    // var longitude;
    // var latitude;
    // var markers;
    // console.log(options.cityName);
  
  },
  /*下拉刷新页面*/
  onPullDownRefresh: function(){
    this._loadData(()=>{
      wx.stopPullDownRefresh()
    });
  },

  //分享效果
  onShareAppMessage: function () {
    return {
      title: '爱车易得',
      path: 'pages/store/store'
    }
  }

})